package com.example.rxpermissiondemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Observable;

/**
 * @author: josan_tang
 * @date: 2018/3/15
 * @description
 */
public class PermissionSplashDialog extends AlertDialog {
    private static final String TAG = "PermissionSplashDialog";
    private PermissionSplashDialog mDialog;
    private Activity mActivity;
    private View.OnClickListener mListener;

    protected PermissionSplashDialog(Activity activity, View.OnClickListener listener) {
        super(activity, R.style.Activity_MyDialog);
        mDialog = this;
        mActivity = activity;
        mListener = listener;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_permission_splash);
        LinearLayout llPermission = findViewById(R.id.ll_permission);
        LinearLayout llDevice = findViewById(R.id.ll_device);
        LinearLayout llStorage = findViewById(R.id.ll_storage);
        LinearLayout llLocation = findViewById(R.id.ll_location);

        List<String> needGrantPermissions = PermissionManager.getInstance().needGrantPermissionList(mActivity, PermissionManager.MUST_GRANTED_PRIMISSION);
        int permissionIconNum = 0;
        for (String needGrantPermission : needGrantPermissions) {
            if (needGrantPermission.contains("LOCATION")) {
                if (llLocation.getVisibility() == View.GONE) {
                    permissionIconNum++;
                    llLocation.setVisibility(View.VISIBLE);
                }
            } else if (needGrantPermission.endsWith("EXTERNAL_STORAGE")) {
                if (llStorage.getVisibility() == View.GONE) {
                    permissionIconNum++;
                    llStorage.setVisibility(View.VISIBLE);
                }
            } else if (needGrantPermission.contains("READ_PHONE_STATE")) {
                permissionIconNum++;
                llDevice.setVisibility(View.VISIBLE);
            }
        }
        if (permissionIconNum < 3) {
            llPermission.setPadding(Utils.dip2px(mActivity, 60), 0 , Utils.dip2px(mActivity, 60), 0);
        }
        TextView tvNext = findViewById(R.id.tv_next);
        tvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                if (mListener != null) {
                    mListener.onClick(v);
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(false);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = (int) (Utils.getScreenWidth(mActivity) * 0.85);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void cancel() {
        super.cancel();
        if (mActivity != null) {
            mActivity.finish();
        }
    }
}
